package com.mao.server.controller;

import java.util.Comparator;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Dict;
import com.mao.server.annotation.AuthAccess;
import com.mao.server.annotation.HoneyLogs;
import com.mao.server.domain.enumuric.LogType;
import com.mao.server.domain.po.Orders;
import com.mao.server.domain.po.User;
import com.mao.server.service.LoginService;
import com.mao.server.service.OrdersService;
import com.mao.server.service.RegisterService;
import com.mao.server.service.UserService;
import com.mao.server.utils.BaseContext;
import com.mao.server.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
public class WebController {
    @Resource
    UserService userService;
    @Resource
    LoginService loginService;

    @Resource
    RegisterService registerService;

    @Resource
    OrdersService ordersService;

    @GetMapping("/test")
    public Result test() {
        return Result.success(BaseContext.getCurrentId());
    }

    @HoneyLogs(operation = "用户", type = LogType.LOGIN)
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        User dbUser = loginService.login(user);
        return Result.success(dbUser);
    }

    @HoneyLogs(operation = "用户", type = LogType.REGISTER)
    @AuthAccess
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        user = registerService.register(user);
        return Result.success(user);
    }

//    @AuthAccess
//    @PutMapping("/resetPassword")
//    public Result resetPassword(@RequestBody User user){
//        userService.resetPassword(user);
//        return Result.success();
//    }
        @GetMapping("/charts")
    public Result charts() {
        // 包装折线图的数据
        List<Orders> list = ordersService.list();
        Set<String> dates = list.stream().map(Orders::getDate).collect(Collectors.toSet());
        List<String> dateList = CollUtil.newArrayList(dates);
        dateList.sort(Comparator.naturalOrder());
        List<Dict> lineList = new ArrayList<>();
        for (String date : dateList) {
            // 统计当前日期的所有金额总数和
            BigDecimal sum = list.stream().filter(orders -> orders.getDate().equals(date)).map(Orders::getMoney)
                    .reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
            Dict dict = Dict.create();
            Dict line = dict.set("date", date).set("value", sum);
            lineList.add(line);
        }

        // 柱状图数据
        List<Dict> barList = new ArrayList<>();
        Set<String> categories = list.stream().map(Orders::getCategory).collect(Collectors.toSet());
        for (String cate : categories) {
            // 统计当前日期的所有金额总数和
            BigDecimal sum = list.stream().filter(orders -> orders.getCategory().equals(cate)).map(Orders::getMoney)
                    .reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
            Dict dict = Dict.create();
            Dict bar = dict.set("name", cate).set("value", sum);
            barList.add(bar);
        }


        // 包装所有数据
        Dict res = Dict.create().set("line", lineList).set("bar", barList);
        return Result.success(res);
    }

}
